
var template = '<p>Hello, my name is <%this.name%>. I\'m <%this.profile.age%> years old.</p>';
var data = {
    name: "Krasimir",
    profile: {
        age: 29
    }
};
// console.log(TemplateEngine(template, data));

function TemplateEngine (tpl, data) {
    var re = /<%([^%>]+)?%>/g;
    while(match = re.exec(tpl)) {
        tpl = tpl.replace(match[0], data[match[1]])
    }
    return tpl;
}

// var fn = new Function("arg", `
// return 'Hello, my name is' + this.name + '. I\'m ' + this.profile.age' years old.</p>
// `);
var tpl = "my name is <%this.name%>. I am <%this.age%> years old."

function demo () {
    return 
    'my name is' + 
    this.name  + 
    '. I am ' + 
    this.profile.age + 
    ' years old.';
}

// var str = "return 'my name is' + this.name + '. I am ' + this.profile.age + ' years old.'";
// console.log(str)
var str = []
str.push('return ')
str.push("'my name is'")
str.push(' + this.name + ')
str.push("'. I am '")
str.push(' + this.profile.age + ')
str.push("' years old.'")

console.log(str.join(''))
var demoFn = new Function(str.join(''))
// console.log(fn.call(data));
console.log(demo.call(data));
console.log(demoFn.call(data));
